package dao;

import java.util.List;

import javax.annotation.Resource;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;

import enumerations.UserType;

import model.Folder;
import model.Membership;

import model.User;

/**
 * @author soreas
 * 
 * Session Bean implementation class MembershipDao
 */
@Stateless
@LocalBean
public class MembershipDao {

	@PersistenceContext
	EntityManager em;

	@Resource
	UserTransaction ut;

    /**
     * Default constructor. 
     */
    public MembershipDao() {
    }
    
    
   
    
    
	/**
	 * Description: Get Membership using repID as folder id
	 * @author Zsolt
	 * @param repID
	 * @return a Membership
	 */
	public Membership getMembershipByRepID(String repID) {
		Membership m = new Membership();
		Folder f =new Folder();
		try {
			f = em.find(Folder.class, Integer.parseInt(repID));
			m = em.createQuery("SELECT m FROM Membership m WHERE m.folder=?1 AND m.supervisor=\"1\" AND m.user.userType=?2",
					Membership.class)
					.setParameter(1, f)
					.setParameter(2, UserType.user)
					.getSingleResult();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return m;
	}

    /**
     * @author soreas
     * 
     * Metoda ce verifica daca un user id si un repo id exista in tabela Membership;
     * 
     * daca exista cu coloana de supervisor=0, atunci schimb coloana supervisor in 1
     * daca nu exista deloc, creez o inregistrare noua cu idul userului, idul repositoryului si coloana supervisor=1
     * 
     * @param pUserId 
     * 				int ce reprezinta idul userului
     * @param pRepoId
     * 				int ce reprezinta idul repositoryului
     */
    public void setSupervisor(int pUserId,int pRepoId){
    	
    	List<Membership> memberships=em.createQuery("select m from Membership m where m.user.id=?1 and m.folder.id=?2 and m.supervisor=0 and m.user.userType=?3", Membership.class)
    							.setParameter(1, pUserId)
    							.setParameter(2, pRepoId)
    							.setParameter(3, UserType.user)
    							.getResultList();
    	if(memberships.size()>0)	// exista legatura, dar cu supervisor 0=>schimb supervisor in 1
    	{
    		try {
				Membership membership=em.find(Membership.class, memberships.get(0).getId());
	    		membership.setSupervisor(1);
	    		em.merge(membership);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			
			}
    		
    	}
    	else			// nu exista leg, deci o inserez, cu supervisor 1
    	{
    		try {
				Membership m=new Membership();
	    		
	    		m.setSupervisor(1);
	  
	    		User u = em.find(User.class,pUserId);
	    		m.setUser((model.User) u);
	    		
	    		Folder fold=em.find(Folder.class, pRepoId);
	    		m.setFolder(fold);
	    		
	    		em.persist(m);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    		
    }
}
